package com.wst.system.server.mapper;

import com.wst.system.server.entity.SysOpenPermission;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author X.I.O
 * @since 2025-01-16
 */
public interface SysOpenPermissionMapper extends BaseMapper<SysOpenPermission> {


    @Select("<script>" +
            "   select a.id, a.api_id,a.app_id,b.api_name,b.api_url,a.createtime from " +
            "       sys_open_permission a left join sys_open_api b on a.api_id=b.id  " +
            "           <where>" +
            "                   AND a.deleted=0 AND a.app_id=#{param.appId}" +
            "                   <if test='param.apiName != null'>" +
            "                           AND b.api_name like CONCAT('%', #{param.apiName}, '%')" +
            "                   </if>" +
            "                   <if test='param.apiUrl != null'>" +
            "                           AND b.api_url like CONCAT('%', #{param.apiUrl}, '%')" +
            "                   </if>" +
            "                   order by a.createtime desc limit #{offset}, #{pageSize}" +
            "           </where>" +
            "</script>")
    List<Map<String,Object>> selectPage(@Param("param") Map<String, Object> param,@Param("offset") int offset,@Param("pageSize") int pageSize);


    @Select("<script>" +
            "   select COUNT(1) from " +
            "       sys_open_permission a left join sys_open_api b on a.api_id=b.id  " +
            "           <where>" +
            "                   AND a.deleted=0 AND a.app_id=#{param.appId}" +
            "                   <if test='param.apiName != null'>" +
            "                           AND b.api_name like CONCAT('%', #{param.apiName}, '%')" +
            "                   </if>" +
            "                   <if test='param.apiUrl != null'>" +
            "                           AND b.api_url like CONCAT('%', #{param.apiUrl}, '%')" +
            "                   </if>" +
            "           </where>" +
            "</script>")
    Integer selectCount(@Param("param") Map<String, Object> param);

}